/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.acceptance.worker;

import java.util.Map;

import org.junit.Before;
import org.junit.Test;
import org.ourgrid.acceptance.util.worker.Req_004_Util;
import org.ourgrid.acceptance.util.worker.Req_088_Util;
import org.ourgrid.acceptance.util.worker.Req_090_Util;
import org.ourgrid.acceptance.util.worker.Req_095_Util;
import org.ourgrid.common.interfaces.to.WorkerStatus;
import org.ourgrid.reqtrace.ReqTest;
import org.ourgrid.worker.WorkerComponent;
import org.ourgrid.worker.WorkerConfiguration;

import br.edu.ufcg.lsd.commune.identification.ContainerID;
import br.edu.ufcg.lsd.commune.identification.DeploymentID;

@ReqTest(reqs="REQ095")
public class Req_095_Test extends WorkerAcceptanceTestCase {
	
	private DeploymentID peerID;
	private Req_004_Util req_004_Util = new Req_004_Util(getComponentContext());
	private Req_090_Util req_090_Util = new Req_090_Util(getComponentContext());
	private Req_095_Util req_095_Util = new Req_095_Util(getComponentContext());
	private Req_088_Util req_088_Util = new Req_088_Util(getComponentContext());
	
	@Before
	public void setUp() throws Exception {
		super.setUp();
		String peerPubKey = workerAcceptanceUtil.simulateAuthentication();
		
		peerID = new DeploymentID(new ContainerID("username", "server", "module", peerPubKey), "peer");
	}

	/** 
	 * This test contains the following steps:
	 * 
	 *    1. Mark time A
	 *    2. Start a Worker with the idleness detector enabled
	 *    3. Get Worker complete status;
	 *           * Mark time B
	 *           * Verify if B-A >= Uptime
	 *           * Verify if the worker status is OWNER
	 *           * Verify is the configuration is equal to the configuration that was previously set
	 *           * Verify if the playpen directory is correct
	 *           * Verify if the storage directory is correct
	 *           * Verify if the master peer is null
	 *    4. Resume the Worker;
	 *           * Verify if the following info message was logged: "Worker has been RESUMED."
	 *           * Verify if the following debug message was logged: "Status changed from OWNER to IDLE."
	 *    5. The master peer sends a setPeer message to the Worker;
	 *           * Verify if the following info message was logged: The peer [peerID] set itself as manager of this Worker.
	 *           * Expect the Worker to send a status changed message to the peer, with the IDLE status
	 *    6. Get Worker complete status;
	 *           * Mark time C
	 *           * Verify if C-A >= Uptime
	 *           * Verify if the worker status is IDLE
	 *           * Verify is the configuration is equal to the configuration that was previously set
	 *           * Verify if the playpen directory is correct
	 *           * Verify if the storage directory is correct
	 *           * Verify if the master peer is [peerID]
	 *    7. The master peer sends a setPeer message to the Worker, with a different ID;
     *           * Verify if the following warn message was logged: "The peer [peerID] set itself as manager of this Worker. This message was ignored. Because the master peer did not notify fail."    
	 *    8. Get Worker complete status;
	 *           * Mark time D
	 *           * Verify if D-A >= Uptime
	 *           * Verify if the worker status is IDLE
	 *           * Verify is the configuration is equal to the configuration that was previously set
	 *           * Verify if the playpen directory is correct
	 *           * Verify if the storage directory is correct
	 *           * Verify if the master peer is [peerID]
	 * 
	 */
	@ReqTest(test=" AT-095.1", reqs="REQ095")
	@Test public void test_ta_095_1_ConsultingWorkerCompleteStatus() throws Exception {
		// Mark time A
		long tickA = System.currentTimeMillis();
		
		// Start the worker with idleness detector
		WorkerComponent component = req_004_Util.startWorker(true);
		
		//TODO Gambiarra :P; o start acima a vezes nao consegue setar direito
		getComponentContext().set(WorkerConfiguration.PROP_IDLENESS_DETECTOR, "yes");
		
		Map<String, String> properties = component.getContainer().getContext().getProperties();
		
		// Get this worker's complete status
		
		String playpenDir = properties.get(WorkerConfiguration.ATT_PLAYPEN_ROOT);
		String storageDir = properties.get(WorkerConfiguration.ATT_STORAGE_DIR);
		
		req_095_Util.getCompleteStatus(component, null,
				WorkerStatus.OWNER, playpenDir, storageDir, tickA);

		// Resume the worker - expect an info and a debug to be logged
		req_088_Util.resumeOwnerWorker(component);
		
		// Master peer sends a setPeer message - expect an info to be logged
		req_090_Util.setKnownPeer(component, peerID, WorkerStatus.IDLE);
		
		// Get this worker's complete status
		req_095_Util.getCompleteStatus(component, peerID, WorkerStatus.IDLE, playpenDir, storageDir, tickA);
		
		// Master peer sends a setPeer message, with another DeploymemtID but the same public key
		DeploymentID peer2ID = new DeploymentID(new ContainerID("username2", "server2", "module2", peerID.getPublicKey()), "peer2");
		
		req_090_Util.setPeerByMasterPeerFromDiffLocation(component, peer2ID, WorkerStatus.IDLE);
		
		// Get this worker's complete status
		req_095_Util.getCompleteStatus(component, peer2ID, WorkerStatus.IDLE, playpenDir, storageDir, tickA);
	}
	
}